﻿using ERPSystem.Common.Domains.PurchaseWarehousing;
using ERPSystem.Common.Domains.SalesOrderCommom;
using ERPSystem.Common.DTO;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SqlSugar;

namespace BasicInformationCommon.API.BasicInformationCommons.Suppliers
{
    public class PurchaseWarehousingSearchHander : IRequestHandler<PurchaseWarehousingSearchCommand, Result<PageResultDto<PurchaseWarehousingDTO>>>
    {
        private readonly ISqlSugarClient _db;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="_db"></param>
        public PurchaseWarehousingSearchHander(ISqlSugarClient _db)
        {
            this._db = _db;
        }

        /// <summary>
        /// 处理采购入库单查询请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<PurchaseWarehousingDTO>>> Handle(PurchaseWarehousingSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<PurchaseWarehousingDTO>>();
            var page = new PageResultDto<PurchaseWarehousingDTO>() { TotalCount = 0, TotalPage = 0, List = new List<PurchaseWarehousingDTO>() };
            try
            {
                RefAsync<int> totaCount = 0;
                RefAsync<int> totalPage = 0;
                var list = _db.Queryable<PurchaseWarehousing, PurchaseIncomingMaterials>((s, sm) => s.SerialNumber == sm.SerialNumber).Select((s, sm) => new PurchaseWarehousingDTO()
                {
                    PurchaseIncomingMaterialsId = sm.PurchaseIncomingMaterialsId,
                    DocumentNumber = s.DocumentNumber,
                    DocumentDate = s.DocumentDate,
                    DocumentStatus = s.DocumentStatus,
                    DocumentType = s.DocumentType,
                    BusinessType = s.BusinessType,
                    BusinessStatus = s.BusinessStatus,
                    BusinessUnit = s.BusinessUnit,
                    BusinessPersonnel = s.BusinessPersonnel,
                    Period = s.Period,
                    Supplier = s.Supplier,
                    Quantity = sm.Quantity,
                    NoteInformation = s.NoteInformation,
                    Volume = sm.Volume,
                    ItemBianma = sm.ItemBianma,
                    MaterialName = sm.MaterialName,
                    TypeSpecification = sm.TypeSpecification,
                    SalesOrder = sm.SalesOrder,
                    LotNumber = sm.LotNumber,
                    Remark = sm.Remark,
                }).MergeTable();
                if (!string.IsNullOrEmpty(request.DocumentNumber)) //单据编码
                {
                    list = list.Where(c => c.DocumentNumber == request.DocumentNumber);
                }
                if (!string.IsNullOrEmpty(request.ItemBianma))//物料编码
                {
                    list = list.Where(c => c.ItemBianma == request.ItemBianma);
                }
                if (!string.IsNullOrEmpty(request.MaterialName))//物料名称
                {
                    list = list.Where(c => c.MaterialName.Contains(request.MaterialName));
                }
                if (!string.IsNullOrEmpty(request.BusinessStatus))//业务状态
                {
                    list = list.Where(c => c.BusinessStatus == request.BusinessStatus);
                }
                if (!string.IsNullOrEmpty(request.DocumentStatus))//单据状态
                {
                    list = list.Where(c => c.DocumentStatus == request.DocumentStatus);
                }
                if (!string.IsNullOrEmpty(request.SalesOrder))
                {
                    list = list.Where(c => c.SalesOrder == request.SalesOrder);
                }
                var res = await list.ToPageListAsync(request.PageIndex, request.PageSize, totaCount, totalPage);
                page.TotalCount = totaCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取当前条件下采购入库单信息成功！";
                }
                else
                {
                    result.Data = new PageResultDto<PurchaseWarehousingDTO>() { List = new List<PurchaseWarehousingDTO>() };
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            return result;
        }
    }
}
